package com.howie.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.howie.gulimall.product.entity.BrandEntity;
import com.howie.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.howie.gulimall.product.entity.CategoryBrandRelationEntity;
import com.howie.gulimall.product.service.CategoryBrandRelationService;
import com.howie.common.utils.PageUtils;
import com.howie.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author laizhenghua
 * @email 3299447929@qq.com
 * @date 2020-12-06 19:27:39
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 获取分类关联的品牌(categoryId)
     * 请求路径：/product/categorybrandrelation/brands/list
     * 请求方式：GET
     * 请求参数：catId 必须要存在！所以我们加上一个required属性
     */
    @GetMapping("/brands/list")
    public R getRelationBrandList(@RequestParam(value = "catId",required = true) Long catId){
        List<BrandEntity> brandEntityList = categoryBrandRelationService.getBrandsByCatId(catId);
        List<BrandVo> brandVoList = brandEntityList.stream().map((item) -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(item.getBrandId());
            brandVo.setBrandName(item.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data",brandVoList);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 查询当前品牌关联的分类API
     * 请求路径：/product/categorybrandrelation/catelog/list
     * 注解@RequestParam：将请求参数绑定到你控制器的方法参数上（是SpringMVC中接收普通参数的注解）
     * method：get
     */
    @RequestMapping(path = "/catelog/list",method = RequestMethod.GET)
    public R catelogList(@RequestParam Long brandId){
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id",brandId);
        List<CategoryBrandRelationEntity> relationList = categoryBrandRelationService.list(wrapper);

        return R.ok().put("data", relationList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存当前品牌与分类关联关系API
     * 请求路径：product/categorybrandrelation/save
     * 请求方式：POST
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){

		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
